from collections import OrderedDict
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


# 127.0.0.1/books/?page=1&page_size=2
# 自定义分页器
class PageNum(PageNumberPagination):

    # 指定每页展示数量的参数
    page_size_query_param = 'page_size'

    # 每页最大返回数量(前端最多能设置的每页数量)
    max_page_size = 5

    # 指定页数参数 默认page
    page_query_param = 'pa'

    # 每页数目
    # page_size = 4

    # 重写父类返回结果的方法，按照指定字段返回内容
    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),
            ('next', self.get_next_link()),
            ('previous', self.get_previous_link()),
            ('data_list', data),
            ('index_page', self.page.number)
        ]))
